import { Product } from '../../pages/product/product-model.js';
var product = new Product();
import { Cart } from '../../pages/cart/cart-model.js';
var cart = new Cart()
Component({
  properties: {
    hiddenAttrBox: {
      type: Boolean,
      value: true
    },
    productDetail:{
      type:Object,
      value:{}
    },
    productCounts:{
      type:Number,
      value:1
    },
    allselect:{
      type:Number,
    }
  },
  data: {

  },
  ready: function () {
  },
  methods: {
    /*切换商品属性*/
    onTabsItemTap: function (event) {
      var productDetail = this.data.productDetail;
      var data = product.changAttr(event, productDetail);
      this.setData({
        'productDetail.attributes': data
      });
      var skusindex = product.changProductinf(this.data.productDetail);
      if (skusindex != undefined) {
        this.setData({
          'allselect': 1,
          'skus': this.data.productDetail.skus[skusindex],
        });

      } else {
        this.setData({
          'allselect': 0,
        });
      }

    },

    /*调整商品数目*/
    onChangeCountsTap: function (event) {
      var maxCount, selected_skus_id;
      if (this.data.allselect == "0") {
        maxCount = this.data.productDetail.product.stock
      } else if (this.data.allselect == "1") {
        maxCount = this.data.skus.stock;
      }
      var currentNum = product.changeCounts(event, this.data.productCounts);
      if (currentNum > maxCount) {
        currentNum = maxCount;
        if (maxCount == 0) {
          currentNum = 1;
          wx.showToast({
            title: '暂无库存~',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '大于库存~',
            icon: 'none',
            duration: 2000
          })
        }
      }
      this.setData({
        'productCounts': currentNum
      })
    },
    //手动输入商品数目
    countChange: function (e) {
      var currentNum = e.detail.value;
      var maxCount, selected_skus_id;
      if (this.data.allselect == "0") {
        maxCount = this.data.productDetail.product.stock
      } else if (this.data.allselect == "1") {
        maxCount = this.data.skus.stock;
      }
      if (currentNum && currentNum < 1) {
        currentNum = 1;
        wx.showToast({
          title: '数量必须是正整数',
          icon: 'none',
          duration: 2000
        })
      }
      if (currentNum > maxCount) {
        currentNum = maxCount;
        if (maxCount == 0) {
          currentNum = 1;
          wx.showToast({
            title: '暂无库存~',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '大于库存~',
            icon: 'none',
            duration: 2000
          })
        }
      }
      this.setData({
        'productCounts': currentNum
      })
    },

    //加入购物车或立即购买
    onAddingToCartTap: function (event) {
      var flag = product.getDataSet(event, 'from')
      if (product.isSelectAll(this.data.productDetail).skustr) {
        this.setData({
          'selectLabel': product.isSelectAll(this.data.productDetail).selectLabel
        })
        var params = {
          'sku_id': product.isSelectAll(this.data.productDetail).skustr,
          'num': this.data.productCounts ? this.data.productCounts : 1,
          'prom_type': this.data.prom_type,
          'prom_id': this.data.prom_id,
        }
        if (flag == 'cart') {
          params.type = 0;
        } else {
          params.type = 1;
        }
        cart.addProduct2Cart(params, (data) => {
          var status = data.status,
            msg = data.msg;
          if (status == 1 && flag == 'cart') {
            this.cancelattrbox();
            wx.showToast({
              title: msg,
              icon: 'none',
              duration: 2000
            })
            this.setData({
              'productCounts': 1,
            });
            var param = {
              num:data.num
            }
            this.triggerEvent('setnum', param);
          } else if (status == 1 && flag == 'buy') {
            wx.navigateTo({
              url: '../order/order?from=buy' + '&id=' + msg
            })
          } else {
            wx.showToast({
              title: msg,
              icon: 'none',
              duration: 2000
            })
          }

        });
      } else {
        this.setData({
          unselectLabel: product.isSelectAll(this.data.productDetail).unselectLabel,
          'selectLabel': false
        })
        if (this.data.productDetail.product.stock == 0) {
          wx.showToast({
            title: '暂无库存！',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '请选择  ' + product.isSelectAll(this.data.productDetail).unselectLabel,
            icon: 'none',
            duration: 2000
          })
        }
      }

    },
    //商品属性弹出框关闭
    cancelattrbox: function (e) {
      if (product.isSelectAll(this.data.productDetail).unselectLabel) {
        this.setData({
          'hiddenAttrBox': true,
          'unselectLabel': product.isSelectAll(this.data.productDetail).unselectLabel,
        })
      } else {
        this.setData({
          'hiddenAttrBox': true,
        })
      }
    },
  }
})